package com.yahoo.music.services
{
	import com.yahoo.music.authentication.KeyAPIManager;
	
	import flash.xml.XMLDocument;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
	import mx.rpc.xml.SimpleXMLDecoder;
	
	public class CategoryServices
	{
		

		private var categoryServices:HTTPService;
		private var categoryServicesArtistsID:HTTPService;
		[Bindable]public var categoryList:XML;
		[Bindable]public var artistByCatId:XML;
		[Bindable]public var categoryDescription:String;
		[Bindable]public var genre:XMLListCollection;
		[Bindable]public var theme:XMLListCollection;
		[Bindable]public var era:XMLListCollection;
		[Bindable]public var categoryArtistsList:XMLListCollection;
		
		public function CategoryServices(){
			super();
		}
		public function listAllCategories():void {
			
			categoryServices = new HTTPService();
			categoryServices.url = "http://us.music.yahooapis.com/category/v1/tree";
			categoryServices.useProxy = false;
			categoryServices.resultFormat ="e4x";
			categoryServices.addEventListener(ResultEvent.RESULT,listCategoriesHandler);
			categoryServices.addEventListener(FaultEvent.FAULT,GenericFault);
			var appid:String;
			categoryServices.send({appid:KeyAPIManager.APP_ID});	
		}
		
		public function listAllArtistByCategoryID(categoryID:String):void {
			var artistsURICatId:String = "http://us.music.yahooapis.com/category/v1/item/"///7318639?response=artists&appid=yO49DfDV34FbEn0PRWB6wUb4Zz4JlxfqOlTYsRcTOPkg9XTjXdk2xxkZhBxLrTohjOgCvEE
			categoryServicesArtistsID = new HTTPService();
			categoryServicesArtistsID.url = artistsURICatId + escape(categoryID)+ "?response=artists,shortdesc";
			categoryServicesArtistsID.resultFormat = "e4x";
			categoryServicesArtistsID.useProxy = false;
			categoryServicesArtistsID.addEventListener(FaultEvent.FAULT,GenericFault);
			categoryServicesArtistsID.addEventListener(ResultEvent.RESULT,ArtistByCategoryHandler);
			var appid:String;
			categoryServicesArtistsID..send({appid:KeyAPIManager.APP_ID});
			
		}
		private function ArtistByCategoryHandler(e:ResultEvent):void {
				artistByCatId = e.result as XML;
				categoryArtistsList = new XMLListCollection();
				categoryArtistsList.source =  artistByCatId.Category.Artist;
				categoryDescription = String(artistByCatId.Category.ShortDescription);
		}
		private function listCategoriesHandler(e:ResultEvent):void{
				categoryList = e.result as XML;
				genre = new XMLListCollection();
				genre.source = categoryList.CategoryType.(@name =='genre').Category;
				theme = new XMLListCollection();
				theme.source = categoryList.CategoryType.(@name =='theme').Category.Category;
				era = new XMLListCollection();
				era.source = categoryList.CategoryType.(@name =='era').Category.Category;
		}
		private function GenericFault(e:FaultEvent):void {
				Alert.show(String(e.fault.message),"error");
		}
		// obsolute function
		private function xmlToObject(value:XML):Object {
                var xmlStr:String = value.toXMLString();
                var xmlDoc:XMLDocument = new XMLDocument(xmlStr);
                var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
                var resultObj:Object = decoder.decodeXML(xmlDoc);
                return resultObj;
         }
		

	}
}